package com.mycompany.rest.client.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.mycompany.rest.client.domain.Artista;
import com.mycompany.rest.client.domain.ArtistaList;
import com.mycompany.rest.client.domain.Destino;
import com.mycompany.rest.client.domain.DestinoList;
import com.mycompany.rest.client.domain.Person;
import com.mycompany.rest.client.domain.PersonList;
import com.mycompany.rest.client.domain.Reserva;
import com.mycompany.rest.client.domain.ReservaList;
import com.mycompany.rest.client.domain.Suscripcion;
import com.mycompany.rest.client.domain.Usuario;
import com.mycompany.rest.client.domain.UsuarioList;
import com.mycompany.rest.util.Writer;

/**
 * REST service client
 */
@Controller
public class RestClientController {

	protected static Logger logger = Logger.getLogger("controller");
	
	private RestTemplate restTemplate = new RestTemplate();
	
	/* Iniciar Aplicacion*/
	@RequestMapping(value = "/inicio", method = RequestMethod.GET)
	public String getInicio(Model model) {
		logger.debug("Received request to show add page");

		// Create new Person and add to model
		// This is the formBackingOBject
		//model.addAttribute("artistaAttribute", new Artista());

		// This will resolve to /WEB-INF/jsp/addpage.jsp
		return "index";
	}
	
	/**/
	@RequestMapping(value = "/consultar", method = RequestMethod.GET)
	public String getConsultar(Model model) {
		logger.debug("Retrieve all persons");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Destino> entity = new HttpEntity<Destino>(headers);

		// Send the request as GET
		try {
			ResponseEntity<DestinoList> result = 
					restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/destinos",
							HttpMethod.GET, entity, DestinoList.class);
			// Add to model
			model.addAttribute("destinos", result.getBody().getData());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/personspage.jsp
		return "consultar";
	}
	
	/**/
	@RequestMapping(value = "/reserva", method = RequestMethod.GET)
	public String getReserva(Model model) {
		logger.debug("Retrieve all persons");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Destino> entity = new HttpEntity<Destino>(headers);
		HttpEntity<Usuario> entity2 = new HttpEntity<Usuario>(headers);

		// Send the request as GET
		try {
			ResponseEntity<DestinoList> result = 
					restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/destinos",
							HttpMethod.GET, entity, DestinoList.class);
			// Add to model
			model.addAttribute("destinos", result.getBody().getData());
			
			ResponseEntity<UsuarioList> result2 = 
					restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/usuarios",
							HttpMethod.GET, entity2, UsuarioList.class);
			// Add to model
			model.addAttribute("usuarios", result2.getBody().getData());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/personspage.jsp
		return "reserva";
	}
	
	/**/
	@RequestMapping(value = "/historial", method = RequestMethod.GET)
	public String getHistorial(Model model) {
		logger.debug("Retrieve all persons");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Reserva> entity = new HttpEntity<Reserva>(headers);

		// Send the request as GET
		try {
			ResponseEntity<ReservaList> result = 
					restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/reservas",
							HttpMethod.GET, entity, ReservaList.class);
			// Add to model
			model.addAttribute("reservas", result.getBody().getData());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/personspage.jsp
		return "historial";
	}
	
	/**
	 * getall
	 */    
	@RequestMapping(value = "/getall", method = RequestMethod.GET)
	public String getAll(Model model) {
		logger.debug("Retrieve all persons");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Artista> entity = new HttpEntity<Artista>(headers);

		// Send the request as GET
		try {
			ResponseEntity<ArtistaList> result = 
					restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artistas",
							HttpMethod.GET, entity, ArtistaList.class);
			// Add to model
			model.addAttribute("artistas", result.getBody().getData());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/personspage.jsp
		return "artista_all";
	}
    
    /**
	 * get
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public String getArtista(@RequestParam("id") Long id, Model model) {
		logger.debug("Retrieve artista with id: " + id);
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Artista> entity = new HttpEntity<Artista>(headers);

		// Send the request as GET
		try {
			ResponseEntity<Artista> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artista/{id}", HttpMethod.GET, entity, Artista.class, id);
			// Add to model
			model.addAttribute("artista", result.getBody());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/getpage.jsp
		return "getpageartista";
	}
	
	/**
     * add
     */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAddPage(Model model) {
		logger.debug("Received request to show add page");

		// Create new Person and add to model
		// This is the formBackingOBject
		model.addAttribute("artistaAttribute", new Artista());

		// This will resolve to /WEB-INF/jsp/addpage.jsp
		return "artista_add";
	}
	
	/**
	 * Sends a new record to the REST provider
	 * based on the information submitted from the JSP add page.
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addArtista(@ModelAttribute("artistaAttribute") Artista artista,
						 Model model) {
		logger.debug("Add new person");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		// Pass the new person and header
		HttpEntity<Artista> entity = new HttpEntity<Artista>(artista, headers);

		// Send the request as POST
		try {
			ResponseEntity<Artista> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artista", HttpMethod.POST, entity, Artista.class);
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/resultpage.jsp
		return "resultpage";
	}
	
	/**
     * Retrieves the JSP update page
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String getUpdatePage(@RequestParam(value="numero", required=true) Integer numero,  
    										Model model) {
    	logger.debug("Received request to show edit page");
    
    	// Retrieve existing Person and add to model
    	// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Artista> entity = new HttpEntity<Artista>(headers);

		// Send the request as GET
		try {
			ResponseEntity<Artista> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artista/{numero}", HttpMethod.GET, entity, Artista.class, numero);
			// Add to model
			model.addAttribute("artistaAttribute", result.getBody());
			
		} catch (Exception e) {
			logger.error(e);
		}
    	
    	// This will resolve to /WEB-INF/jsp/updatepage.jsp
    	return "artista_update";
	}
	
	/**
	 * Sends an update request to the REST provider
	 * based on the information submitted from the JSP update page.
	 */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateArtista(@ModelAttribute("artistaAttribute") Artista artista,
						@RequestParam(value="numero",  required=true) Integer numero,
						Model model) {
		logger.debug("Update existing person");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		// Pass the new person and header
		HttpEntity<Artista> entity = new HttpEntity<Artista>(artista, headers);
		
		// Send the request as PUT
		ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artista/{numero}", HttpMethod.PUT, entity, String.class, numero);

		// This will resolve to /WEB-INF/jsp/resultpage.jsp
		return "resultpage";
    }
    
    /**
	 * Sends a delete request to the REST provider
	 */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deletePerson(@RequestParam("numero") Long numero,
										Model model) {
    	logger.debug("Delete existing person");
    	
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
    	HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<String> entity = new HttpEntity<String>(headers);	
		
		// Send the request as DELETE
		ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/artista/{numero}", HttpMethod.DELETE, entity, String.class, numero);
		
		// This will resolve to /WEB-INF/jsp/resultpage.jsp
		return "resultpage";
    }


    
    
    
    
    
    
    
    @RequestMapping(value = "/suscripcion", method = RequestMethod.GET)
	public String suscripcion(Model model) {
		logger.debug("Received request to show add page");

		// Create new Person and add to model
		// This is the formBackingOBject
		model.addAttribute("suscripcionAttribute", new Suscripcion());

		// This will resolve to /WEB-INF/jsp/addpage.jsp
		return "suscripcion_add";
	}
    
    
    @RequestMapping(value = "/suscripcion", method = RequestMethod.POST)
	public String addPerson(@ModelAttribute("suscripcionAttribute") Suscripcion suscripcion,
						 Model model) {
		logger.debug("Add new person");
		
		// Prepare acceptable media type
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		// Prepare header
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		// Pass the new person and header
		HttpEntity<Suscripcion> entity = new HttpEntity<Suscripcion>(suscripcion, headers);

		// Send the request as POST
		try {
			ResponseEntity<Suscripcion> result = restTemplate.exchange("http://localhost:8080/spring-rest-servidor/pc02/suscripcion", HttpMethod.POST, entity, Suscripcion.class);
		} catch (Exception e) {
			logger.error(e);
		}
		
		// This will resolve to /WEB-INF/jsp/resultpage.jsp
		return "resultpage";
	}
    
}
